var ConnectManager = function(){

	this.connectionsLimit = 10;

		this.getName = function(){
		return this.name;
		}

		this.decreaseConnections = function(){
			if(this.connectionsLimit>0){
			this.connectionsLimit--;
			}
		}
		this.increaseConnections = function(){
			this.connectionsLimit++;
		}
		this.setConnections = function(con){	
			this.connectionsLimit=con;
		}
		this.getConnections = function(){	
		return this.connectionsLimit;
		}

	this.interact = function(tpf,entities){

		var time = new Date();
		var nodeCount = entities.length;
		
		// Radius increase function parameters (exponential).
		var xAtEnd = document.getElementById("x").value*1;
		var yAtX = document.getElementById("y").value*1;
		var aAtY = document.getElementById("a").value*1;
		
		// Radius function parts
		var a = (yAtX-aAtY*xAtEnd)/(-(xAtEnd*xAtEnd));
		var b = aAtY-2*xAtEnd*a;
		var x = 0;
				
		
		var lastRad2 = 0;
		var currentRad;
		var currentRad2;
		
		while(x<xAtEnd)
		{
		currentRad = x*(a*x+b);
		x++;
		currentRad2=currentRad*currentRad;
			
			// Each node is not checked agains every other node each time since a connection only goes one way or the other, therefore i=0 and j = i+1.
			// This reduces cost.
			
			for(i = 0;i<nodeCount;i++){
//			document.write(nodeCount+":"+this.connectionsLimit+":"+connectI.connectedEntitiesTracker+"<br>");
			var connectI = entities[i].connectComponent;	
				if(connectI.connectedEntitiesTracker<this.connectionsLimit)
				{
				
					for(j = i+1;j<nodeCount;j++){
					var connectJ = entities[j].connectComponent;
						if(connectJ.connectedEntitiesTracker<this.connectionsLimit)
						{
							if(connectI.distanceBetweenEntities[j] >= lastRad2 && connectI.distanceBetweenEntities[j] < currentRad2)
							{							
								if(connectI.connectedEntitiesTracker<this.connectionsLimit)
								{
								connectI.connectedEntities[connectI.connectedEntities.length] = j;
								connectI.connectedEntitiesTracker++;	// Not nescassary to put this outside of if statement since there is no reason to increase if it's > this.connectionsLimit
								}
							connectJ.connectedEntitiesTracker++;	// Even if a node wasn't connected it should have been, so we count this as connected to avoid entities that are further away from being connected, no reason to put in if statement, since this increase cost.
							}
						}
						else if (connectI.connectedEntitiesTracker<this.connectionsLimit){
							if(connectI.distanceBetweenEntities[j] >= lastRad2 && connectI.distanceBetweenEntities[j] < currentRad2)
							{
							connectI.connectedEntitiesTracker = this.connectionsLimit;
							}
						}
					}
				}
				else{
					for(j = i+1;j<nodeCount;j++){
						if (entities[j].connectComponent.connectedEntitiesTracker<this.connectionsLimit){
							if(connectI.distanceBetweenEntities[j] >= lastRad2 && connectI.distanceBetweenEntities[j] < currentRad2)
							{
							entities[j].connectComponent.connectedEntitiesTracker = this.connectionsLimit;
							}													
						}
					}
				
				}
								
			}
			
		lastRad2=currentRad2;		
		}
	}
}